<?php
class Zing_Cache_ZingGlobalCache
{
	static $_cache_array = array();
	static $_no_of_instance = null;
	static $properties = array();
	private static $_default_instance = 'default';

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $instance
	 * @return Zing_Cache_ZingCache
	 */
	public static function getGlobalCache($instance = 'default')
	{
		return self::getCacheInstance($instance);
	}

	/*
	 
	 );
	 *
	 */

	/**
     *
     * @param unknown_type $instance
     * @return Zing_Cache_ZingCache
	 * config co dang giong trong configuration theo stdClass
	 * $conf = new stdClass();
	 * $conf->type = "purememcache";
	 * $conf->enable = true;
	 * $conf->host = "10.30.12.155";
	 * $conf->port = "11211";
	 *
        */
	public static function getCacheInstanceByConfig($instancename,$config) {
		
		if(empty($instancename)) {
			throw new Exception("instance name must not be empty");
		}

		$cache = "cfg." . $instancename;

		if (array_key_exists($cache, self::$_cache_array)) {
			return self::$_cache_array[$cache];
		}

		if($config == null || $config->type == null) {			
			$memcache = new Zing_Cache_NoCache();
			self::$_cache_array[$cache] = $memcache;
		}
		else {
			if (!isset($config->type))
				$type = 'nocache';
			else {
				$type = $config->type;
			}
			self::getCacheAdapter($cache, $type, $config);
		}
		return self::$_cache_array[$cache];
	}

	/**
	*
	* @param unknown_type $instance
	* @return Zing_Cache_ZingCache
	*/
	public static function getCacheInstance($instance = 'default')
	{
		$config = Zend_Registry::get('configuration');
		$cache = $instance;
		if (!isset($config->cachingfarm->$cache)) {
			$cache = 'default';
			$instance = $cache;
		}
		
		if (array_key_exists($instance, self::$_cache_array)) {
			return self::$_cache_array[$instance];
		}
		
		$enable = true;
        if (SGN_Application::$params['nocache'] || isset($_GET['nocache'])) {
            $enable = false;
        }
		if(isset($config->cachingfarm->$cache) && $enable)
		{
			$con = $config->cachingfarm->$cache;
			if(!isset($con->type))
				$type = 'zingcache';
			else {
				$type = $con->type;
			}
			self::getCacheAdapter($cache, $type, $con);
		}
		else
		{
			$memcache = new Zing_Cache_NoCache();
			self::$_cache_array[$cache] = $memcache;
		}
		
		return self::$_cache_array[$cache];
	}

	public static function getCacheAdapter($cache, $type, $con) {
		switch($type) {
			case "apc"; {
					$_enable = $con->enable;
					if ($_GET['apcCache'] == 1) {
					    $_enable = true;
					}
					$debug = Globals::isDebug();
					if (empty($debug))
						$debug = false;
					if ($_enable) {
						//echo "i'm here with $cache - $_enable - $_server - $_port <br>";
						$apccache = new Zing_Cache_APC($debug);
						self::$_cache_array[$cache] = $apccache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
			case "purememcache": {
					$_enable = $con->enable;
					$_server = $con->host;
					$_port = $con->port;
					$debug = Globals::isDebug();

					if (empty($debug))
						$debug = false;

					if ($_enable) {

						$warmup = null;
						if (isset($con->warmup->enable) && isset($con->warmup->instance) && $con->warmup->enable == true) {
							$warmup = $con->warmup->instance;
						}
						$memcache = new Zing_Cache_PureMemcache($_server, $_port, $debug, $warmup);
						self::$_cache_array[$cache] = $memcache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
			case "nocache": {
					$memcache = new Zing_Cache_NoCache();
					self::$_cache_array[$cache] = $memcache;
					break;
				}
			case "memcache": {
					$_enable = $con->enable;
					$_hosts = $con->hosts;
					$_hosts_array = explode(',', $_hosts);
					$servers = array();
					for ($j = 0; $j < count($_hosts_array); $j++) {
						$tmp = explode(':', $_hosts_array[$j]);
						$servers[] = array(
							'host' => $tmp[0],
							'port' => $tmp[1]
						);
					}
                                        
					$debug = Globals::isDebug();

					if (empty($debug))
						$debug = false;

					if ($_enable) {
						$warmup = null;
						if (isset($con->warmup->enable) && isset($con->warmup->instance) && $con->warmup->enable == true) {
							$warmup = $con->warmup->instance;
						}

						$compress_threshold = null;
						if (isset($con->compress_threshold)) {
							$compress_threshold = $con->compress_threshold;
						}
						$memcache = new Zing_Cache_Memcache($servers, $debug, $warmup, $compress_threshold);
						self::$_cache_array[$cache] = $memcache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
			case "xcache": {
					$_enable = $con->enable;
					$_read = $con->read->toArray();
					$_write = $con->write->toArray();
					if (isset($con->keyprefix))
						$_keyprefix = $con->keyprefix;
					else
						$_keyprefix = '';

					$compress_level = 0;
					if (isset($con->xcache->compress->enable) && isset($con->xcache->compress->level) && $con->xcache->compress->enable) {
						$compress_level = intval($con->xcache->compress->level);
					}

					$debug = Globals::isDebug();

					if (empty($debug))
						$debug = false;

					if ($_enable) {

						$warmup = null;
						if (isset($con->warmup->enable) && isset($con->warmup->instance) && $con->warmup->enable == true) {
							$warmup = $con->warmup->instance;
						}
						$memcache = new Zing_Cache_XCacheService($_read, $_write, $_keyprefix, $debug, $warmup, $compress_level);
						//$memcache = new Zing_Cache_ZingCache($_server, $_port, $debug, $warmup);
						self::$_cache_array[$cache] = $memcache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
			case "xcachev2": {
					$_enable = $con->enable;
					$_read = $con->read->toArray();
					$_write = $con->write->toArray();
					if (isset($con->keyprefix))
						$_keyprefix = $con->keyprefix;
					else
						$_keyprefix = '';

					$compress_threshold = -1;
					if (isset($con->xcache->compress->enable) && isset($con->xcache->compress->threshold) && $con->xcache->compress->enable) {
						$compress_threshold = intval($con->xcache->compress->threshold);
					}

					$debug = Globals::isDebug();

					if (empty($debug))
						$debug = false;

					if ($_enable) {

						$warmup = null;
						if (isset($con->warmup->enable) && isset($con->warmup->instance) && $con->warmup->enable == true) {
							$warmup = $con->warmup->instance;
						}

						$memcache = new Zing_Cache_XCacheServiceV2($_read, $_write, $_keyprefix, $debug, $warmup, $compress_threshold);
						//$memcache = new Zing_Cache_ZingCache($_server, $_port, $debug, $warmup);
						self::$_cache_array[$cache] = $memcache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
			case "xcachev3": {
					$_enable = $con->enable;
					$_read = $con->read->toArray();
					$_write = $con->write->toArray();
					if (isset($con->keyprefix))
						$_keyprefix = $con->keyprefix;
					else
						$_keyprefix = '';

					$compress_threshold = -1;
					if (isset($con->xcache->compress->enable) && isset($con->xcache->compress->threshold) && $con->xcache->compress->enable) {
						$compress_threshold = intval($con->xcache->compress->threshold);
					}

					$debug = Globals::isDebug();

					if (empty($debug))
						$debug = false;

					if ($_enable) {

						$warmup = null;
						if (isset($con->warmup->enable) && isset($con->warmup->instance) && $con->warmup->enable == true) {
							$warmup = $con->warmup->instance;
						}

						$memcache = new Zing_Cache_XCacheServiceV3($_read, $_write, $_keyprefix, $debug, $warmup, $compress_threshold);
						self::$_cache_array[$cache] = $memcache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
			case "zingcache":
			default: {
					$_enable = $con->enable;
					$_server = $con->host;
					$_port = $con->port;
					$debug = Globals::isDebug();

					if (empty($debug))
						$debug = false;

					if ($_enable) {

						$warmup = null;
						if (isset($con->warmup->enable) && isset($con->warmup->instance) && $con->warmup->enable == true) {
							$warmup = $con->warmup->instance;
						}
						$memcache = new Zing_Cache_ZingCache($_server, $_port, $debug, $warmup);
						self::$_cache_array[$cache] = $memcache;
					} else {
						$memcache = new Zing_Cache_NoCache();
						self::$_cache_array[$cache] = $memcache;
					}
					break;
				}
		}
	}

	public static function getAllProfilerData()
	{

		$output = "";

		//$config = Zend_Registry::get('configuration');

		//if(isset($config->cachingfarm->list))
		if(true)
		{
			//$list = $config->cachingfarm->list;
			//$arr = explode(',', $list);
			$arr = array_keys(self::$_cache_array);

			$config = Zend_Registry::get('configuration');
			if(isset($config->cachingfarm->list)) {
				$list = $config->cachingfarm->list;
				$arr_list = explode(',', $list);
				if(is_array($arr_list) && !empty($arr_list)) {
					$arr += $arr_list;
				}
			}

			if(is_array($arr) && count($arr) > 0)
			{
				for($i=0;$i<count($arr);$i++)
				{
					$cache = $arr[$i];
					if(array_key_exists($cache, self::$_cache_array))
					{
						$output .= self::$_cache_array[$cache]->getProfilerData("'" . $cache . "'");
					}
					else
					{
						$output .= "Cache instance '$cache' not initialized";
					}
					$output .= "<br><br>";
				}
			}
		}

		return $output;
	}


	public static function flushLocalCache($instance = '')
	{
		$cache = Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
		if($cache != null) $cache->flushLocalCache();
	}

	 public static function getMultiCache($keys, $instance = '')
	 {
	 	$cache = Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
	 	$result = FALSE;
	 	if($cache != null)
	 	{
	 		$result = $cache->getMultiCache($keys);
	 	}
	 	return $result;
	 }

	public static function getCache($key, $instance = '')
	{
		$cache = Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
		$result = FALSE;
		if($cache != null)
		{
			$result = $cache->getCache($key);
		}
		return $result;
	}

    public static function increment($key,$instance='',$value = 1)
    {
        $cache = Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
        if(empty($cache) || !method_exists($cache,"increment") )
                return false;
        return $cache->increment($key,$value);
    }

	public static function deleteCache($key, $instance = '')
	{
		$cache = Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
		if($cache != null)
		{
			$cache->deleteCache($key);
		}
	}

	public static function setCache($key, $value, $instance = '', $expireTime = -1,  $compress=0)
	{
		$cache = Zing_Cache_ZingGlobalCache::getGlobalCache($instance);
		if($cache != null)
		{
			$cache->setCache($key,$value,$expireTime,$compress);
		}
	}


	//public static function setCache($key, )




}
?>
